package com.itfreer.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.itfreer.metadata.dictionary.DictionaryManage;
import com.itfreer.ui.base.api.IChildData;
import com.itfreer.ui.base.api.IDataView;
import com.itfreer.ui.base.api.IEditTextValidator;
import com.itfreer.ui.base.api.ISimpleView;
import com.itfreer.ui.base.api.OnValueChange;
import com.itfreer.ui.base.validations.EditTextValidator;
import com.itfreer.ui.base.validations.ValidationExecutor;
import com.itfreer.ui.base.validations.ValidationModel;
import com.itfreer.ui.base.validations.ValidationRegister;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 提供基础表单Activity，重点解决表的数据录入问题
 */
public abstract class FormActivity extends BaseActivity {

    private EditTextValidator editTextValidator;

    public EditTextValidator getEditTextValidator(){
        return editTextValidator;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        editTextValidator = new EditTextValidator(this);

        initValidator();
    }

    /**
     * 初始化验证器
     */
    protected void initValidator(){
        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);

        for(View item : viewList){
            if(item instanceof IEditTextValidator){
                IEditTextValidator editTextValidator = (IEditTextValidator)item;
                String validatorName = editTextValidator.getValidatorName();
                if(validatorName!=null && validatorName!=""){
                    ValidationExecutor alidationExecutor = ValidationRegister.getValidationExecutor(validatorName);
                    if(alidationExecutor!=null){
                        getEditTextValidator().add(new ValidationModel((EditText) item, alidationExecutor, editTextValidator.getValidateTitle()));
                    }
                }
            }
        }
        getEditTextValidator().execute();
    }

    /**
     * 初始化绑定字典域
     */
    protected void initBindDomain(){
        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);

        for(View item : viewList){
            if(item instanceof IDataView){
                IDataView simpleView = (IDataView)item;
                // 绑定字典
                bindDomain(simpleView);
                // 设置子节点
                if(simpleView.getNextUI()!=null && !simpleView.getNextUI().equals("")){
                    int intialResID = getResources().getIdentifier(simpleView.getNextUI(), "id", getApplicationContext().getPackageName());
                    final IDataView cDataView = (IDataView) findViewById(intialResID);
                    simpleView.setOnValueChange(new OnValueChange() {
                        @Override
                        public void onChange(String value) {
                            cDataView.setPid(value);
                            bindDomain(cDataView);
                        }
                    });
                }
                if(simpleView.getNextUIEx()!=null){
                    final IDataView cDataView = simpleView.getNextUIEx();
                    simpleView.setOnValueChange(new OnValueChange() {
                        @Override
                        public void onChange(String value) {
                            cDataView.setPid(value);
                            bindDomain(cDataView);
                        }
                    });
                }
            }
        }
    }

    protected void bindDomain(IDataView simpleView) {
        String fieldName = simpleView.getFieldName();
        if(fieldName==null || fieldName.equals("")){
            return;
        }

        String dicName = simpleView.getDicName();
        if(dicName==null || dicName.equals("")){
            return;
        }

        if(simpleView.getLoadLevel()!=null && simpleView.getLoadLevel()>1) {
            Map<String, Object> datas = DictionaryManage.instance.getChildrensEx(dicName, simpleView.getPid(), simpleView.getLoadLevel(),false);
            simpleView.setBindData(datas);
        } else {
            Map<String, Object> datas = DictionaryManage.instance.getChildEx(dicName, simpleView.getPid(),false);
            simpleView.setBindData(datas);
        }
    }

    /**
     * 绑定数据
     * @param dataMap
     */
    protected void setDataBind(Map dataMap){
        if(dataMap==null){
            return;
        }
        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);

        for(final View item : viewList){
            if(item instanceof ISimpleView){
                ISimpleView simpleView = (ISimpleView)item;
                String fieldName = simpleView.getFieldName();
                if(fieldName!=null && fieldName!="" && dataMap.containsKey(fieldName)){
                    Object value = dataMap.get(fieldName);
                    simpleView.setFieldValue(value);
                }
            } else if(item instanceof IChildData){
                IChildData child = (IChildData)item;
                final String tableName = child.getChildTableName();
                Map<String, Map> childTables = new HashMap<String, Map>();
                Map childTable = new HashMap();
                Map orderObj = new HashMap();
                orderObj.put(child.getOrderFiledName(), child.getOrderType());
                childTable.put("orderObject", orderObj);
                childTables.put(tableName, childTable);
            }
        }
    }

    /**
     * 获取数据
     */
    protected Map getDataBind(Map<String, Object> data){
        if(data==null) {
            data = new HashMap<String, Object>();
        }

        View view = getRootView(this);
        List<View> viewList = getAllChildViews(view);

        for(View item : viewList){
            if(item instanceof ISimpleView){
                ISimpleView simpleView = (ISimpleView)item;
                String fieldName = simpleView.getFieldName();
                if(fieldName!=null && fieldName!=""){
                    data.put(fieldName, simpleView.getFieldValue());
                }
            } else if(item instanceof IChildData){
                IChildData child = (IChildData)item;
                String tableName = child.getChildTableName();
                onGetChildValues(item, data);
            }
        }
        return data;
    }

    /**
     * 设置子表数据
     */
    protected void onSetChildValues(View item, Map dataMap){

    }

    /**
     * 获取子表数据
     */
    protected void onGetChildValues(View item, Map dataMap){

    }
}